package com.fch.server.service;

import com.fch.server.domain.*;
import com.fch.server.dto.RoleDto;
import com.fch.server.dto.PageDto;
import com.fch.server.mapper.RoleMapper;
import com.fch.server.mapper.RoleResourceMapper;
import com.fch.server.mapper.RoleUserMapper;
import com.fch.server.util.CopyUtil;
import com.fch.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: fangchuhao
 **/
@Service
public class RoleService {
    private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleResourceMapper roleResourceMapper;

    @Resource
    private RoleUserMapper roleUserMapper;

    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        RoleExample roleExample = new RoleExample();
        List<Role> roles = roleMapper.selectByExample(roleExample);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        pageDto.setTotal(rolePageInfo.getTotal());
        List<RoleDto> roleDtos = CopyUtil.copyList(roles, RoleDto.class);
        pageDto.setList(roleDtos);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(RoleDto roleDto) {
        Role role = CopyUtil.copy(roleDto,Role.class);
        if (StringUtils.isEmpty(role.getId())) {
            insert(role);
        }else {
            update(role);
        }
    }

    /**
     * 新增
     */
    private void insert(Role role) {
        role.setId(UuidUtil.getShortUuid());
        roleMapper.insert(role);
    }

    /**
     * 更新
     */
    private void update(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    /**
     * 删除
     */
    public void delete(String id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 保存资源
     * @param roleDto
     */
    public void saveResource(RoleDto roleDto) {
        String roleId = roleDto.getId();
        List<String> resourceIds = roleDto.getResourceIds();
        // 清空当前角色的所有资源
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleResourceMapper.deleteByExample(example);

        // 保存角色资源
        for (int i = 0; i < resourceIds.size(); i++) {
            RoleResource roleResource = new RoleResource();
            roleResource.setId(UuidUtil.getShortUuid());
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceIds.get(i));
            roleResourceMapper.insert(roleResource);
        }
    }

    public List<String> listResource(String roleId) {
        RoleResourceExample example = new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleResource> roleResources = roleResourceMapper.selectByExample(example);
        List<String> resourceIds = new ArrayList<>();
        for (RoleResource roleResource : roleResources) {
            resourceIds.add(roleResource.getResourceId());
        }
        return resourceIds;
    }

    public void saveUser(RoleDto roleDto) {
        String roleId = roleDto.getId();
        List<String> userIds = roleDto.getUserIds();

        // 先清空角色关联用户的全部数据
        RoleUserExample example = new RoleUserExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleUserMapper.deleteByExample(example);

        // 保存角色用户
        for (int i = 0; i < userIds.size(); i++) {
            RoleUser roleUser = new RoleUser();
            roleUser.setId(UuidUtil.getShortUuid());
            roleUser.setRoleId(roleId);
            roleUser.setUserId(userIds.get(i));
            roleUserMapper.insert(roleUser);
        }
    }

    public List<String> listUser(String roleId) {
        RoleUserExample example = new RoleUserExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleUser> roleUsers = roleUserMapper.selectByExample(example);
        List<String> userIds = new ArrayList<>();
        for (int i = 0; i < roleUsers.size(); i++) {
            userIds.add(roleUsers.get(i).getUserId());
        }
        return userIds;
    }
}
